package com.zllkj.mm.module.moment

import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.contract.OnDatePickedListener
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout
import com.previewlibrary.GPreviewBuilder
import com.zllkj.mm.R
import com.zllkj.mm.R2
import com.zllkj.mm.api.network.RetrofitHelper
import com.zllkj.mm.api.network.RxHelper
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber
import com.zllkj.mm.base.BaseTitleFragment
import com.zllkj.mm.common.L
import com.zllkj.mm.model.entity.DynamicCommentVo
import com.zllkj.mm.model.entity.GridImageViewBean
import com.zllkj.mm.module.goods.VideoPlayerDetailedActivity
import com.zllkj.mm.module.shopping.vm.ItemFriendListVM
import com.zllkj.mm.util.common.FormatUtil
import com.zllkj.mm.util.common.PictureLoadUtil
import kotlinx.android.synthetic.main.fragment_friend_detail.view.*
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.*
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.ivhead
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.ll_invite_time_area
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.rv_nine_data
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.tvUserName
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.tv_age_and_sex
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.tv_friend_invite_money
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.tv_friend_invite_time
import kotlinx.android.synthetic.main.fragment_friend_freetime_invite.view.tv_item_friend_content


class FreeTimeInviteFragment(val vm: ItemFriendListVM) : BaseTitleFragment() {

    var startTime = ""
    var endTime = ""

    override fun getContentLayoutId(): Int {
        return R.layout.fragment_friend_freetime_invite
    }

    override fun loadData() {
        super.loadData()

        loadCarryDataFromList()
    }

    fun loadCarryDataFromList(){
        var gridView = mRootView.rv_nine_data
        if(vm.imageList.size == 1 && vm.imageList[0].url == "") {
            gridView.visibility = View.GONE
        }
        gridView.layoutManager = GridLayoutManager(requireContext(),3, GridLayoutManager.VERTICAL,false)
        var adapter = object : BaseQuickAdapter<GridImageViewBean, BaseViewHolder>(R.layout.item_9_friend_image,vm.imageList){
            override fun convert(p0: BaseViewHolder?, p1: GridImageViewBean?) {
                p1?.let {
                    if (it.url != null) {
                        p0?.getView<AppCompatImageView>(R.id.iv_nine_image)?.let { it1 ->
                            PictureLoadUtil.loadPic(it1, p1.url!!)
                        }
                    }
                    if (it.videoUrl != null) {
                        p0?.getView<AppCompatImageView>(R.id.iv_video_play_icon)?.visibility = View.VISIBLE
                        p0?.getView<AppCompatImageView>(R.id.iv_nine_image)?.let { it1 ->
                            PictureLoadUtil.loadImageFromOnLineUrl(it1, p1.url!!)
                        }
                    }

                }
            }
        }
        gridView.adapter = adapter
        adapter.setOnItemClickListener { baseQuickAdapter, view, i ->
            if(!TextUtils.isEmpty( vm.imageList[i].videoUrl)){

                val intent = Intent(requireActivity(), VideoPlayerDetailedActivity::class.java)
                intent.putExtra("url", vm.imageList[i].videoUrl)
                requireActivity().startActivity(intent)
            } else {
                GPreviewBuilder.from(requireActivity())
                        .setData(vm.imageList)
                        .setIsScale(true)
                        .setCurrentIndex(i)
                        .setType(GPreviewBuilder.IndicatorType.Dot)
                        .start() //启动
            }

        }

        vm.userDyBean?.let {
            var ivHead = mRootView.ivhead
            mRootView.tvUserName.text = it.nikeName
            mRootView.tv_item_friend_content.text = it.content

            var address = "|" + it.province + it.city + it.district
            mRootView.tv_age_and_sex.text = if(it.sex == 1)"男" else "女"  + "|" + it?.province + it.city + it.district
            mRootView.tv_age_and_sex.append(address)
            PictureLoadUtil.loadPic(ivHead,vm.userDyBean!!.userLogo)
            it.price?.let { price ->
                mRootView.ll_invite_time_area.visibility = View.VISIBLE
                mRootView.tv_friend_invite_money.text = FormatUtil.bigDecimalUtilFormat(price,"100") + "/次"
                mRootView.tv_friend_invite_time.text = FormatUtil.dataFormat(it.createTime)
            }
        }

        mRootView.tv_free_time_select_start_time.setOnClickListener {
            showTimePicker(0)
        }
        mRootView.tv_free_time_select_end_time.setOnClickListener {
            showTimePicker(1)
        }
    }

    fun showTimePicker(type:Int){
        val picker = DatePicker(requireActivity())
        picker.setBodyWidth(240)
        val wheelLayout: DateWheelLayout = picker.getWheelLayout()
        wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)

        wheelLayout.setDefaultValue(DateEntity.today())
        wheelLayout.setDateLabel("年", "月", "日")
        wheelLayout.setCurtainEnabled(true)
        wheelLayout.setCurtainColor(Color.WHITE)
        wheelLayout.setIndicatorEnabled(true)
        wheelLayout.setIndicatorColor(Color.BLUE)
        wheelLayout.setTextColor(Color.BLACK)
        wheelLayout.setSelectedTextColor(Color.BLUE)
        wheelLayout.yearLabelView.setTextColor(Color.BLACK)
        wheelLayout.monthLabelView.setTextColor(Color.BLACK)
        wheelLayout.dayLabelView.setTextColor(Color.BLACK)
        picker.setOnDatePickedListener(object : OnDatePickedListener{
            override fun onDatePicked(year: Int, month: Int, day: Int) {
                if (type == 0) {
                    startTime = year.toString() + "-" + month + "-" + day + " 00:00:00"
                    mRootView.tv_free_time_select_start_time.text = year.toString() + "年" + month + "月" + day + "日"
                } else {
                    mRootView.tv_free_time_select_end_time.text = year.toString() + "年" + month + "月" + day + "日"
                    endTime = year.toString() + "-" + month + "-" + day + " 00:00:00"

                }
            }

        })
        picker.show()

        mRootView.btn_free_time_invite_confirm.setOnClickListener {
            invite()
        }
    }

    fun invite(){
        if (TextUtils.isEmpty(startTime) || TextUtils.isEmpty(endTime)) run {
            L.showToast("请选择时间")
            return
        }

        addSubscribe(RetrofitHelper.getInstance().dynamicApi.addInvite((HashMap<String, Any>().apply {
            this["startTime"] = startTime
            this["endTime"] = endTime
            this["dynamicId"] = vm.userDyBean?.dynamicId.toString()
        })).compose<String>(RxHelper.defaultHandleResult<String>()).subscribe(object : ProgressSubscriber<String>(context) {
            override fun onSucc(t: String?) {
                removeFragment()
                L.showToast("邀请成功")
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
            }
        }))
    }
}